home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d3 / rettig.arc / TRSOURCE.EXE / FATTRIB.C < prev    next >
C/C++ Source or Header  |  1990-10-22  |  3KB  |  120 lines

  1. /*********
  2. *  Utility: FATTRIB.C
  3. *  by Leonard Zerman, Ralph Davis
  4. * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
  5. *
  6. *  Syntax:  FATTRIB <expC1> <expC2> ... <expCn>
  7. *
  8. *  Note:    Parameters are as follows, in any order:
  9. *
  10. *           Filename, including extension
  11. *
  12. *           Attributes, preceded by + to turn on or - to turn off
  13. *              may be entered individually or together, i.e.:
  14. *
  15. *                  FATTRIB FILE.TXT -R -A +H
  16. *                  FATTRIB FILE.TXT -RA +H
  17. *
  18. *           Attributes are:
  19. *
  20. *              A--archive
  21. *              H--hidden
  22. *              R--read-only
  23. *              S--system
  24. *********/
  25.  
  26. #include "stdio.h"
  27. extern char * FIND_FIRST();
  28. extern char * FIND_NEXT();
  29.  
  30. void main(argc,argv)
  31. int argc;
  32. char *argv[];
  33. {
  34.     static char filename[65];
  35.     static char attribs[] = "           "; /* 11 spaces */
  36.  
  37.     int  i = 0, j;           /* indexes into attribute string and *argv[] */
  38.     int  file;               /* file descriptor */
  39.     int  status;             /* returns success or failure of operation */
  40.     char *fnameptr;
  41.  
  42.     /* Check for valid number of arguments --get info if not entered */
  43.  
  44.     if (argc == 1)
  45.     {
  46.         fprintf(stderr,"\n\nPlease enter filename:  ");
  47.         gets(filename);
  48.  
  49.         fprintf(stderr,"\n\nPlease enter attributes (A/H/R/S):  ");
  50.         gets(&attribs[1]);
  51.  
  52.         fprintf(stderr,"\n\nPlease enter operation (+/-):  ");
  53.         attribs[0] = getchar();
  54.  
  55.     } else
  56.  
  57.     if (argc == 2)
  58.     {
  59.         if ((argv[1][0] == '+') || (argv[1][0] == '-'))
  60.         {
  61.             for (j = 0; argv[1][j]; j++)
  62.                 attribs[i++] = argv[1][j];
  63.             fprintf(stderr,"\n\nPlease enter filename:  ");
  64.             gets(filename);
  65.         }
  66.         else
  67.         {
  68.             strncpy(filename, argv[1], 64);
  69.             fprintf(stderr,"\n\nPlease enter attributes (A/H/R/S):  ");
  70.             gets(&attribs[1]);
  71.  
  72.             fprintf(stderr,"\n\nPlease enter operation (+/-):  ");
  73.             attribs[0] = getchar();
  74.  
  75.         }
  76.     } else
  77.  
  78.     {
  79.         while (--argc)
  80.         {
  81.             /* Is parameter the filename?  Check first character
  82.                for + or - */
  83.  
  84.             if ((argv[argc][0] == '+') || (argv[argc][0] == '-'))
  85.             {
  86.                 for (j = 0; argv[argc][j]; j++)
  87.                     attribs[i++] = argv[argc][j];
  88.             }
  89.             else
  90.             {
  91.                 strncpy(filename, argv[argc], 64);
  92.             }
  93.         }
  94.         attribs[i] = '\0';
  95.     }
  96.  
  97.     fnameptr = FIND_FIRST(filename);
  98.     while(fnameptr)
  99.     {
  100.        file = open(fnameptr, 0);
  101.    
  102.        if (file == -1)                   /* File not found */
  103.        {
  104.            fprintf(stderr, "\007File %s not found\n", fnameptr);
  105.            exit(1);
  106.        }
  107.        close(file);
  108.        status = FILEATTR(fnameptr, attribs);
  109.        
  110.        if (status == 0)
  111.           fprintf(stderr, "Attributes of file %s successfully changed\n\n",
  112.                   fnameptr);
  113.        else
  114.           fprintf(stderr, "\007Error changing attributes of file %s\n\n",
  115.                   fnameptr);
  116.        fnameptr = FIND_NEXT();
  117.     }
  118.  
  119. }
  120.